<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="单号" prop="num">
          <el-input-number v-model="queryParams.num" placeholder="请输入单号" />
        </el-form-item>
       <el-form-item label="区域" prop="deptId">
        <select-input dsCode="sys_dept_community" v-model="queryParams.deptId" style="width:180px"/>
      </el-form-item>     
      <el-form-item label="收费项目" prop="feeType">
        <el-select v-model="queryParams.feeType" placeholder="请选择收费项目" clearable style="width:120px">
          <el-option
            v-for="dict in fee_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> 
      <el-form-item label="房屋" prop="roomId">
        <el-input v-model="queryRoomName" placeholder="请选择" readonly @click="doSelectRoom('query')"/>
        <el-input v-model="queryParams.roomId" type="hidden"/>
      </el-form-item>
      <el-form-item label="支付方式" prop="payWay">
        <el-select v-model="queryParams.payWay" placeholder="请选择" clearable style="width:120px">
          <el-option
            v-for="dict in pay_way"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width:120px">
          <el-option
            v-for="dict in charge_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="收费日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeStartDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单号" align="center" prop="num" />
      <el-table-column label="区域" align="center" prop="deptName" />
      <el-table-column label="房屋" align="center" prop="roomId">
        <template #default="scope">
          {{scope.row.buildingName}} - {{scope.row.unitNum}} - {{scope.row.roomName}}
        </template>
      </el-table-column>
      <el-table-column label="收费项目" align="center" prop="feeType">
        <template #default="scope">
          <dict-tag :options="fee_type" :value="scope.row.feeType"/>
        </template>
      </el-table-column>
      <el-table-column label="收费标准" align="center" prop="itemName" />
      <el-table-column label="折扣" align="center" prop="discountName" />
      <el-table-column label="应收金额" align="center" prop="feeShould" />
      <el-table-column label="实收金额" align="center" prop="feeReal" />
      <el-table-column label="退费金额" align="center" prop="feeReturn" />
      <el-table-column label="支付方式" align="center" prop="payWay">
        <template #default="scope">
          <dict-tag :options="pay_way" :value="scope.row.payWay"/>
        </template>
      </el-table-column>
      <el-table-column label="缴费期限" align="center" prop="startDate">
        <template #default="scope">
          {{scope.row.startDate}} ~ {{scope.row.endDate}}
        </template>
      </el-table-column>
      <el-table-column label="收费时间" align="center" prop="createTime" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="charge_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Money" @click="handleReturn(scope.row)" v-if="scope.row.status != '0' && scope.row.status != '9'" v-hasPermi="['service:charge:edit']">退费</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收费记录对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-tabs v-model="tab">
          <el-tab-pane label="退款信息" name="returns">  
      <el-form ref="returnRef" :model="form" :rules="rules" label-width="80px">  
        <el-form-item label="退费项目" prop="feeType">
          <dict-tag :options="fee_type" :value="form.feeType"/>
        </el-form-item>
        <el-form-item label="区域" prop="deptId">
          <el-input v-model="form.deptId" type="hidden"/>
          <el-input v-model="chargeInfo.deptName" readonly placeholder="区域" />
        </el-form-item>      
        <el-form-item label="房屋" prop="roomId" v-if="form.roomId">
          <el-input :value="chargeInfo.buildingName + ' - ' + chargeInfo.unitNum + ' - ' + chargeInfo.roomName" readonly placeholder="请输入房屋" />
          <el-input v-model="form.roomId" type="hidden"/>
        </el-form-item>     
        <el-form-item label="车辆" prop="carId" v-if="form.carId">
          <el-input :value="chargeInfo.carPlate" readonly placeholder="请输入车辆" />
          <el-input v-model="form.carId" type="hidden"/>
        </el-form-item>
        <el-form-item label="退费金额" prop="feeReturn">
          <el-input-number v-model="form.feeReturn"  placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="退款方式" prop="payWay">
        <el-select v-model="form.payWay" placeholder="请选择" clearable>
          <el-option
            v-for="dict in pay_way"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="单据状态" prop="chargeStatus">
          <el-select v-model="form.chargeStatus" placeholder="请选择">
            <el-option
              v-for="dict in charge_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退款说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明" />
        </el-form-item>
      </el-form>
          </el-tab-pane>
          <el-tab-pane label="收款信息" name="charge">  
      <el-form ref="chargeRef" :model="chargeInfo" label-width="80px">  
        <el-form-item label="收费标准" prop="itemId">
          <el-input v-model="chargeInfo.itemId" type="hidden"/>
          <el-input v-model="chargeInfo.itemName" readonly placeholder="收费标准" />
        </el-form-item>
        <el-form-item label="折扣" prop="discountId">
          <el-input v-model="chargeInfo.discountId" type="hidden"/>
          <el-input v-model="chargeInfo.discountName" readonly placeholder="折扣" />
        </el-form-item>
        <el-form-item label="缴费月数" prop="feeMonth">
          <el-input v-model="chargeInfo.feeMonth" readonly/>
        </el-form-item>
        <el-form-item label="缴费期限" prop="startDate">
          {{chargeInfo.startDate}} ~ {{chargeInfo.endDate}}
        </el-form-item>
        <el-form-item label="应收金额" prop="feeShould">
          <el-input v-model="chargeInfo.feeShould" readonly placeholder="请输入应收金额" />
        </el-form-item>
        <el-form-item label="实收金额" prop="feeReal">
          <el-input v-model="chargeInfo.feeReal" readonly placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="已退金额" prop="feeReturn">
          <el-input v-model="chargeInfo.feeReturn" readonly />
        </el-form-item>
        <el-form-item label="单据说明" prop="remark">
          <el-input v-model="chargeInfo.remark" readonly />
        </el-form-item>
      </el-form>
          </el-tab-pane>
        </el-tabs>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <room-select :show="showRoomSelect" @select="selectRoom" :params="roomSelectParams"/>
  </div>
</template>

<script setup name="ChargeReturn">
import { getToken } from "@/utils/auth";
import { listCharge, getCharge, delCharge } from "@/api/service/charge";
import { addChargeReturn } from "@/api/service/chargeReturn";
import { getRoom } from "@/api/archive/room";
import SelectInput from "@/components/FormInput/Select";
import RoomSelect from "@/components/Selection/Room";

const { proxy } = getCurrentInstance();
const { pay_way, fee_type, charge_status } = proxy.useDict('pay_way', 'fee_type', 'charge_status');

const chargeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeStartDate = ref([]);
const formRoomName = ref("");
const queryRoomName = ref("");
const showRoomSelect = ref(false);
const roomSelectParams = ref(null);
const chargeItemOptions = ref([]);
const chargeDiscountOptions = ref([]);
const discountInfo = ref(null);
const tab = ref("returns");
const chargeInfo = ref('');

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    deptId: null,
    roomId: null,
    carId: null,
    itemId: null,
    feeType: null,
    discountId: null,
    payWay: null,
    startDate: null,
    endDate: null,
    status: null
  },
  rules: {
    deptId: [
      { required: true, message: "区域不能为空", trigger: "blur" }
    ],
    feeType: [
      { required: true, message: "收费项目不能为空", trigger: "blur" }
    ],
    feeReturn: [
      { required: true, message: "退款金额不能为空", trigger: "blur" }
    ],
    payWay: [
      { required: true, message: "支付方式不能为空", trigger: "change" }
    ],
    chargeStatus: [
      { required: true, message: "单据状态不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询收费记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeStartDate && '' != daterangeStartDate) {
    queryParams.value.params["beginStartDate"] = daterangeStartDate.value[0];
    queryParams.value.params["endStartDate"] = daterangeStartDate.value[1];
  }
  listCharge(queryParams.value).then(response => {
    chargeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function doSelectRoom(params){
  roomSelectParams.value = params;
  showRoomSelect.value = true;
}
function selectRoom(room){
  showRoomSelect.value = false;
  if(room == null){
    form.value.roomId = null;
    formRoomName.value = null;
    queryParams.value.roomId = null;
    queryRoomName.value = null;
    return;
  }
  if(room.params == 'form'){
    form.value.roomId = room.id;
    formRoomName.value = room.name;
  }else{
    queryParams.value.roomId = room.id;
    queryRoomName.value = room.name;
  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    roomId: null,
    carId: null,
    feeType: null,
    chargeId: null,
    feeReturn: null,
    payWay: null,
    remark: null,
    chargeStatus: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("returnRef");
  tab.value = 'returns';
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.roomId = null;
  queryRoomName.value = null;
  daterangeStartDate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleReturn(row) {
  reset();
  const _id = row.id || ids.value
  getCharge(_id).then(response => {
    chargeInfo.value = response.data;
    
    form.value = {
      id: null,
      deptId: response.data.deptId,
      roomId: response.data.roomId,
      carId: response.data.carId,
      feeType: response.data.feeType,
      chargeId: response.data.id,
      feeReturn: (response.data.feeType == 'deposit' ? response.data.feeReal : null),
      payWay: response.data.payWay,
      remark: null,
      chargeStatus: null
    };

    open.value = true;
    title.value = "退费";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["chargeRef"].validate(valid => {
    if (valid) {
        addChargeReturn(form.value).then(response => {
        proxy.$modal.msgSuccess("记录退款成功");
        open.value = false;
        getList();
      });
    }
  });
}

getList();
</script>
